package org.jeecg.common.wx;


import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.SecureRandom;

public class Encryption {
    public static final String MD5 = "MD5";
    public static final String SHA1 = "SHA1";
    public static final String HmacMD5 = "HmacMD5";
    public static final String HmacSHA1 = "HmacSHA1";
    public static final String DES = "DES";
    public static final String AES = "AES";

    /**
     * 编码格式；默认使用uft-8
     */
    public String charset = "utf-8";
    /**
     * DES
     */
    public int keysizeDES = 0;
    /**
     * AES
     */
    public int keysizeAES = 128;

    public static Encryption me;

    public Encryption() {
        //单例
    }

    //双重锁
    public static Encryption getInstance() {
        if (me == null) {
            synchronized (Encryption.class) {
                if (me == null) {
                    me = new Encryption();
                }
            }
        }
        return me;
    }

    /**
     * 使用Base64进行加密
     *
     * @param res 密文
     * @return
     */
    public static String Base64Encode(String res) {
        return Base64.encode(res.getBytes());
    }

    /**
     * 使用Base64进行解密
     *
     * @param res
     * @return
     */
    public static String Base64Decode(String res) {
        return new String(Base64.decode(res));
    }


    public static void main(String[] args) {

        String a = Base64Encode("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
        String b = Base64Decode("XlthLXpBLVowLTlfLV0rQFthLXpBLVowLTlfLV0rKFwuW2EtekEtWjAtOV8tXSspKyQ=");

        System.out.println(b);
    }
}